//
//  PKDetailTableViewController.m
//  PianKe
//
//  Created by mingtao on 15/10/4.
//  Copyright (c) 2015年 mingtao. All rights reserved.
//

#import "PKDetailTableViewController.h"
#import "PKRadioMainModelDetail.h"

#import "PKRequestManager.h"

#import "PKRadioMainDetailTableViewCell.h"

#import "PKRadioDetail.h"
#import "PKRadioDetailTextTableViewCell.h"

#import "PKPlayViewController.h"


@interface PKDetailTableViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSMutableArray *NsArray;

@property(nonatomic,strong)NSMutableArray *NsDetailArray;

@property(nonatomic,strong)UITableView *DetailTableView;//主页面

@property(nonatomic,strong)UIView *topView;
@property(nonatomic,strong)UIButton *but;
@property(nonatomic,strong)NSMutableArray *Nsarrays;


@end

@implementation PKDetailTableViewController

-(NSMutableArray *)Nsarrays
{
    if (_Nsarrays == nil) {
        _Nsarrays = [NSMutableArray arrayWithCapacity:0];
    }
    
    return _Nsarrays;
}

-(NSMutableArray *)NsDetailArray
{
    if (_NsDetailArray == nil) {
        _NsDetailArray = [NSMutableArray array];
    }
    
    return _NsDetailArray;
}

-(NSMutableArray *)NsArray
{
    if (_NsArray ==nil) {
        _NsArray = [NSMutableArray array];
    }
    return _NsArray;

}


//[PKRequestManager requestWithType:POST urlString:@"http://api2.pianke.me/ting/radio_detail" patDic:parDic finish:^(NSData *data) {
//    NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
//    NSArray *listArray=dict[@"data"][@"list"];
//    for(NSDictionary *modelDict in listArray)
//    {
//        PKRadioDetailModel *detailModel=[[PKRadioDetailModel alloc]init];
//        [detailModel setValuesForKeysWithDictionary:modelDict];
//        [self.dataSourceArray addObject:detailModel];
//        
//    }
//    [self.tableView reloadData];
//    PKRadioDetailHeaderView *detailHeaderView=[[PKRadioDetailHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
//    NSDictionary *raioInfoDict=dict[@"data"][@"radioInfo"];
//    NSLog(@"%@",raioInfoDict);
//    PKRadioInfo *radioInfoModel=[[PKRadioInfo alloc]init];
//    [radioInfoModel setValuesForKeysWithDictionary:raioInfoDict];
//    detailHeaderView.radioInfo=radioInfoModel;
//    self.tableView.tableHeaderView=detailHeaderView;
//    
//} Error:^(NSError *error) {
//    NSLog(@"详情界面的数据请求失败：%@",error);
//}];


//requestDta
-(void) DetailRequestDta
{
  NSMutableDictionary *parDic=[NSMutableDictionary dictionaryWithCapacity:0];

    parDic[@"auth"]=@"";
    parDic[@"deviceid"]=@"63A94D37-33F9-40FF-9EBB-481182338873";
    parDic[@"radioid"]=self.Did;
    parDic[@"version"]=@"3.0.4";
    //PKRequestManager

    [PKRequestManager requestWithType:POST urlString:@"http://api2.pianke.me/ting/radio_detail" parDic:parDic finish:^(NSData *data) {
        NSDictionary *Dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        for (NSDictionary *keyDict in Dic[@"data"][@"list"])
        {
            PKRadioDetail *RadioModelDetail=[[PKRadioDetail alloc]init];
            [RadioModelDetail setValuesForKeysWithDictionary:keyDict];
            
            [self.NsDetailArray addObject:RadioModelDetail];
        }
        
        
        
        NSDictionary *Dicts=Dic[@"data"][@"radioInfo"];
        
        PKRadioMainDetailTableViewCell *radiDetail = [[PKRadioMainDetailTableViewCell alloc]initWithFrame:CGRectMake(0, 70, WIHES, HHIHEI/4+120)];

        PKRadioMainModelDetail *RadioModelDetail=[[PKRadioMainModelDetail alloc]init];
        
        [RadioModelDetail setValuesForKeysWithDictionary:Dicts];
        
        [self.NsArray addObject:RadioModelDetail];
        
        radiDetail.array = self.NsArray;
        
        self.DetailTableView.tableHeaderView =radiDetail;
        //self.mainTableView.tableHeaderView= pkRadio;
        
        
        
//       // for (NSString *DictDetailKey in Dic[@"data"][@"authorinfo"]) {
//        NSDictionary *Radiodict=Dic[@"data"][@"authorinfo"];
//            PKRadioDetail *RadioModelDetail1=[[PKRadioDetail alloc]init];
//        [RadioModelDetail1 setValuesForKeysWithDictionary:Radiodict];
//            [self.NsDetailArray addObject:RadioModelDetail];
//       // }
        
        [_DetailTableView reloadData];

    } error:^(NSError *error) {
        
         NSLog(@"电台详细界面请求数据失败%@",error);
    }];


    
}







- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    
    _topView =[[UIView alloc]initWithFrame:CGRectMake(0, 20, WIHES, 44)];
    
    _topView.backgroundColor = [UIColor blackColor];
    
    _but=[UIButton buttonWithType:(UIButtonTypeSystem)];
    
    _but.frame = CGRectMake(30, 10, 30, 30);
    
    [_but setBackgroundImage:[[UIImage imageNamed:@"ImageBut"] imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)] forState:UIControlStateNormal];
    
    [_but addTarget:self action:@selector(butImage) forControlEvents:UIControlEventTouchUpInside];
    
    [_topView addSubview:_but];
    
    
    _DetailTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 70, WIHES,HHIHEI-70)];
    
    _DetailTableView.dataSource =self;
    _DetailTableView.delegate = self;
    
    _DetailTableView.rowHeight =HHIHEI/4-85;// HHIHEI/4+10;
    
  //  _DetailTableView.separatorStyle =UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_DetailTableView];
    [self.view addSubview:_topView];
    
    
     [_DetailTableView registerClass:[PKRadioDetailTextTableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"];
    
        [self DetailRequestDta];
    
    
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.NsDetailArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   // PKRadioMainDetailTableViewCell
   PKRadioDetailTextTableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
    
    
    cell.readiDetail = self.NsDetailArray[indexPath.row];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    PKPlayViewController *playVC=[[PKPlayViewController alloc]init];
    
    for (int i = 0 ; i<_NsDetailArray.count; i++) {
        PKRadioDetail *molde = _NsDetailArray[i];
        
        if (i==indexPath.row) {
            molde.isSelected = YES;
        }else
        {
            molde.isSelected = NO;
        }
    }
    
        playVC.datalistSourceArray = self.NsDetailArray;
    
    playVC.radioDetailModel=self.NsDetailArray[indexPath.row];
    
    
    for (int i = 0; i<self.NsDetailArray.count; i++) {
        
        NSString *musicUrl=((PKRadioDetail *)self.NsDetailArray[i]).musicUrl;
        [self.Nsarrays addObject:musicUrl];
        
    }
    playVC.arr=self.Nsarrays;
    playVC.Subscript=indexPath.row;
    
    
    
    
    [self.navigationController pushViewController:playVC animated:YES];

}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)butImage
{
    [self.navigationController popViewControllerAnimated:YES];

}

@end
